/*
 * 太原科技大学。
 * Copyright 2021, YCHS Information&Science Technology Co.,Ltd,
 * All rights reserved.
 */
package com.example.demo.controller;

import com.example.demo.Utils.JWTUtils;
import com.example.demo.pojo.PageBean;
import com.example.demo.pojo.Plan;
import com.example.demo.pojo.Result;
import com.example.demo.service.PlanService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author liuchao
 * @version 1.0
 * @date 2024/5/5
 */
@Slf4j
@RestController
public class planController {

    @Autowired
    private PlanService planService;

    //获取计划
    @GetMapping("/plan")
    public Result getData(String token){

        log.info("token");
        Object username = JWTUtils.getJWT(token).get("username");
        Object userid = JWTUtils.getJWT(token).get("userid");
        //将token中包含的id信息转换为int类型进行查询
        int id= Integer.parseInt((String) userid);
        PageBean plan= planService.selectUserID(username,id);
        System.out.println(username);
        System.out.println(id);

        return Result.success(plan);
    }
    //删除计划
    @DeleteMapping("/plan/delete")
    public Result deleatId(Integer id){

        log.info("id");
        System.out.println(id);
        System.out.println(id.getClass().toString());
        planService.deleatId(id);
        return Result.success();
    }
    //添加计划
    /*
    2024/09/15测试bug，前端请求的数据为空时没有对应处理方案
     */
    @PostMapping("/plan/add")
    public Result planAdd(@RequestBody Plan plan){
        log.info("添加计划");
        System.out.println(plan.getPName());
        if (plan.getPName()==null){
            return Result.error("计划为空");
        }

        Plan rePlan=planService.addPlan(plan);

        return Result.success(rePlan);
    }
}
